<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>Vue路由history实现原理</h1>
    <button id="btn">切换url</button>
</body>
</html>
<script>
    const btn = document.getElementById('btn')
    // 1、页面初始化加载 获取url
    window.addEventListener('DOMContentLoaded',()=>{
        console.log('path:',location.pathname);
    })
    // 2、点击事件切换url
    btn.addEventListener('click',()=>{
        const state = {name:'user'}
        history.pushState(state,'','user')
        console.log('切换路由到user');
    })
    // 点击浏览器前进后退监听
    window.onpopstate = (e)=>{
        console.log(e.state,location.pathname);
    }
</script>